package com.cd.dao;

import com.cd.entity.Classes;
import com.cd.entity.Student;
import com.cd.mapper.ClassesMapper;
import com.cd.mapper.StudentMapper;
import com.cd.util.MybatisUtil;
import com.cd.util.PageResult;
import com.cd.vo.StudentVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.Style;
import java.io.IOException;
import java.util.List;

public class StudentDao {
//    1.调用sql语句查出学生
    public List<Student> findAll() throws IOException {
        try (SqlSession session = MybatisUtil.getSqlSession(true)) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            List<Student> students = mapper.findAll();
            for (Student student : students){
                //取出学生所在班级编号
                Integer cid = student.getCid();
                //根据班级编号查询出班级对象
                Classes classes = classesMapper.findById(cid);
                //为学生设置班级名称
                student.setCname(classes.getCname());
            }
            return students;
        }
    }
    //分页查询

    public PageResult<Student> findByPage(int page, int pageSize) throws IOException{
        //开始分页
        PageHelper.startPage(page,pageSize);
        //查询得到总学生
        List<Student> students = findAll();
        //讲结果转换为Page<Student>对象
        Page<Student> pageStudents = (Page<Student>) students;
        //返回构造的PageResult对象
        return new PageResult<>(pageStudents.getTotal(),pageStudents.getResult());
    }
    //分页查询条件
    public PageResult<Student> search(int page, int pageSize, StudentVo studentVo) throws IOException {
//        1. 开始分页
        PageHelper.startPage(page,pageSize);
        //开始分页查询
        try(SqlSession session = MybatisUtil.getSqlSession(true)) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
            List<Student> students = mapper.search(studentVo);

            for (Student student : students){
                //取出学生所在班级编号
                Integer cid = student.getCid();
                //根据班级编号查询出班级对象
                Classes classes = classesMapper.findById(cid);
                //为学生设置班级名称
                student.setCname(classes.getCname());
            }
            //讲结果转换为Page<Student>对象
            Page<Student> pageStudents = (Page<Student>) students;
            //返回构造的PageResult对象
            return new PageResult<>(pageStudents.getTotal(),pageStudents.getResult());
        }
    }
//添加学生
    public void add(Student student) throws IOException{
        try(SqlSession session = MybatisUtil.getSqlSession(true)) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.add(student);
        }

    }
//修改学生
    public void update(Student student) throws IOException{
        try(SqlSession session = MybatisUtil.getSqlSession(true)) {
            StudentMapper mapper = session.getMapper(StudentMapper.class);
            mapper.update(student);
        }
    }
}
